package com.tvunetworks.center.device.service.impl;

import com.tvunetworks.center.common.util.MyUtil;
import com.tvunetworks.center.device.mapper.DeviceResoureRelationMapper;
import com.tvunetworks.center.device.model.DeviceResoureRelation;
import com.tvunetworks.center.device.model.MyDevice;
import com.tvunetworks.center.device.service.DeviceResoureRelationService;
import com.tvunetworks.center.device.service.MyDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @program: tvucc-aggregator
 * @description: DeviceResoureRelationImpl
 * @author lebronchen
 * @create: 2020-01-09 12:58
 **/
@Service
@Slf4j
public class DeviceResoureRelationServiceImpl implements DeviceResoureRelationService {

    @Value("${gridPack}")
    private String gridPack;
    @Value("${gridSdi}")
    private String gridSdi;

    @Autowired
    private DeviceResoureRelationMapper deviceResoureRelationMapper;
    @Autowired
    private MyDeviceService myDeviceService;
    @Override
    public Set<String> copyR(String oldPeerId, String newPeerId, String taskId) {
        // 复制 R
        copyReceiver(oldPeerId, newPeerId, taskId);
        Set<String> result = new HashSet<>();
        // 复制 X
        Set<String> ridList1 = copyGrid(oldPeerId, newPeerId, gridPack, taskId);
        if (!CollectionUtils.isEmpty(ridList1)) {
            result.addAll(ridList1);
        }
        // 复制 X
        Set<String> ridList2 = copyGrid(oldPeerId, newPeerId, gridSdi, taskId);
        if (!CollectionUtils.isEmpty(ridList2)) {
            result.addAll(ridList2);
        }
        return result;
    }

    // 复制 R 配对 Grid
    private void copyReceiver(String oldPeerId, String newPeerId, String taskId){
        // Old Relations
        List<DeviceResoureRelation> oldRelations = deviceResoureRelationMapper.select(new DeviceResoureRelation().setRId(oldPeerId));
        if (CollectionUtils.isEmpty(oldRelations)) {
            log.error("Start copyDevice DeviceResoureRelation R oldRelations null taskId:[{}] oldPeerId:[{}] newPeerId:[{}]", taskId, oldPeerId, newPeerId);
            return;
        }

        // Old 和 New 有 相同 peerId，resourceId 的 new Relation 的主键 Ids
        List<String> newIds = deviceResoureRelationMapper.listNewIdsContainBothGrid(oldPeerId, newPeerId);
        if (!CollectionUtils.isEmpty(newIds)) {
            int count = deviceResoureRelationMapper.deleteByIdList(newIds);
            log.error("Start copyDevice DeviceResoureRelation R deleteRelation taskId:[{}] oldPeerId:[{}] newPeerId:[{}] deleteNum:[{}] deleteIds:[{}]", taskId, oldPeerId, newPeerId, count, newIds);
        }

        for (DeviceResoureRelation relation : oldRelations) {
            relation.setId(MyUtil.getUUID())
                    .setRId(newPeerId);
        }
        int count = deviceResoureRelationMapper.insertList(oldRelations);
        log.error("Start copyDevice DeviceResoureRelation R need copy list taskId:[{}] oldPeerId:[{}] newPeerId:[{}] copyNum:[{}] copyList:[{}]", taskId, oldPeerId, newPeerId, count, oldRelations);
        return;
    }

    // 复制 R的X 配对的 R
    private Set<String> copyGrid(String oldPeerId, String newPeerId, String resourceId, String taskId){
        MyDevice oldGrid = myDeviceService.getDeviceByPeerId(oldPeerId);
        if (oldGrid == null) {
            log.error("Start copyDevice copyGrid failed DeviceResoureRelation X not exist taskId:[{}] oldPeerId:[{}] newPeerId:[{}]", taskId, oldPeerId, newPeerId);
            return null;
        }
        MyDevice newGrid = myDeviceService.getDeviceByPeerId(newPeerId);
        if (newGrid == null) {
            log.error("Start copyDevice copyGrid failed DeviceResoureRelation X not exist taskId:[{}] oldPeerId:[{}] newPeerId:[{}]", taskId, oldPeerId, newPeerId);
            return null;
        }
        // Old Relations
        List<DeviceResoureRelation> oldRelations = deviceResoureRelationMapper.select(new DeviceResoureRelation().setPeerId(oldPeerId).setResourceId(resourceId));
        if (CollectionUtils.isEmpty(oldRelations)) {
            log.error("Start copyDevice DeviceResoureRelation X oldRelations is null taskId:[{}] oldPeerId:[{}] newPeerId:[{}]", taskId, oldPeerId, newPeerId);
            return null;
        }

        // Old 和 New 有 相同 rId 的 new Relation 的主键 Ids
        List<String> newIds = deviceResoureRelationMapper.listNewIdsContainBothRid(oldPeerId, newPeerId, resourceId);
        if (!CollectionUtils.isEmpty(newIds)) {
            int count = deviceResoureRelationMapper.deleteByIdList(newIds);
            log.error("Start copyDevice DeviceResoureRelation X deleteRelation taskId:[{}] oldPeerId:[{}] newPeerId:[{}] deleteNum:[{}] deleteIds:[{}]", taskId, oldPeerId, newPeerId, count, newIds);
        }

        for (DeviceResoureRelation relation : oldRelations) {
            relation.setId(MyUtil.getUUID())
                    .setPeerId(newPeerId);
        }
        int count = deviceResoureRelationMapper.insertList(oldRelations);
        log.error("Start copyDevice DeviceResoureRelation R need copy list taskId:[{}] oldPeerId:[{}] newPeerId:[{}] copyNum:[{}] copyList:[{}]", taskId, oldPeerId, newPeerId, count, oldRelations);
        Set<String> rids = oldRelations.stream().map(item -> item.getRId()).collect(Collectors.toSet());
        return rids;

    }
}
